﻿using System;
using UnityEngine;

namespace ParticlePlayground
{
	// Token: 0x02000166 RID: 358
	[Serializable]
	public class SkinnedWorldObject : WorldObjectBaseC
	{
		// Token: 0x06000860 RID: 2144 RVA: 0x000319A8 File Offset: 0x0002FBA8
		public virtual void Initialize()
		{
			this.gameObject = this.transform.gameObject;
			this.cachedId = this.gameObject.GetInstanceID();
			this.rigidbody = this.gameObject.GetComponent<Rigidbody>();
			this.renderer = this.transform.GetComponentInChildren<SkinnedMeshRenderer>();
			this.mesh = this.renderer.sharedMesh;
			if (this.mesh != null)
			{
				this.normals = this.mesh.normals;
				this.vertices = new Vector3[this.mesh.vertices.Length];
				this.localVertices = this.mesh.vertices;
				this.weights = this.mesh.boneWeights;
				this.boneTransforms = this.renderer.bones;
				this.bindPoses = this.mesh.bindposes;
				this.boneMatrices = new Matrix4x4[this.boneTransforms.Length];
				this.vertexPositions = this.vertices;
				this.initialized = true;
				this.Update();
			}
			else
			{
				global::Debug.Log("No mesh could be found in the assigned Skinned World Object. Make sure a mesh is assigned to your Skinned Mesh Renderer, if so - make sure the hierarchy under only have one Skinned Mesh Renderer component.", this.gameObject);
			}
		}

		// Token: 0x06000861 RID: 2145 RVA: 0x00031ACC File Offset: 0x0002FCCC
		public virtual void MeshUpdate()
		{
			if (this.initialized)
			{
				if (this.boneMatrices != null && this.boneMatrices.Length > 0)
				{
					this.localVertices = this.mesh.vertices;
				}
				if (this.updateNormals)
				{
					this.normals = this.mesh.normals;
				}
			}
		}

		// Token: 0x06000862 RID: 2146 RVA: 0x00031B2C File Offset: 0x0002FD2C
		public virtual void BoneUpdate()
		{
			if (this.initialized)
			{
				if (this.boneMatrices != null && this.boneMatrices.Length > 0)
				{
					for (int i = 0; i < this.boneMatrices.Length; i++)
					{
						if (this.boneTransforms[i] != null)
						{
							this.boneMatrices[i] = this.boneTransforms[i].localToWorldMatrix * this.bindPoses[i];
						}
					}
				}
				else
				{
					if (this.bakedMesh == null)
					{
						this.bakedMesh = new Mesh();
					}
					this.renderer.BakeMesh(this.bakedMesh);
					this.localVertices = this.bakedMesh.vertices;
					this.vertexMatrix.SetTRS(this.renderer.transform.position, this.renderer.transform.rotation, this.renderer.transform.localScale);
				}
			}
		}

		// Token: 0x06000863 RID: 2147 RVA: 0x00031C3C File Offset: 0x0002FE3C
		public virtual void Update()
		{
			if (this.initialized)
			{
				for (int i = 0; i < this.vertices.Length; i++)
				{
					if (this.boneMatrices.Length > 0)
					{
						Matrix4x4 matrix4x = this.boneMatrices[this.weights[i].boneIndex0];
						Matrix4x4 matrix4x2 = this.boneMatrices[this.weights[i].boneIndex1];
						Matrix4x4 matrix4x3 = this.boneMatrices[this.weights[i].boneIndex2];
						Matrix4x4 matrix4x4 = this.boneMatrices[this.weights[i].boneIndex3];
						for (int j = 0; j < 16; j++)
						{
							this.vertexMatrix[j] = matrix4x[j] * this.weights[i].weight0 + matrix4x2[j] * this.weights[i].weight1 + matrix4x3[j] * this.weights[i].weight2 + matrix4x4[j] * this.weights[i].weight3;
						}
					}
					this.vertices[i] = this.vertexMatrix.MultiplyPoint3x4(this.localVertices[i]);
				}
				this.vertexPositions = this.vertices;
			}
			this.isDoneThread = true;
		}

		// Token: 0x06000864 RID: 2148 RVA: 0x00031DD8 File Offset: 0x0002FFD8
		public virtual void UpdateOnNewThread()
		{
			if (this.isDoneThread)
			{
				this.isDoneThread = false;
				PlaygroundC.RunAsync(delegate()
				{
					if (this.isDoneThread)
					{
						return;
					}
					this.Update();
				});
			}
		}

		// Token: 0x06000865 RID: 2149 RVA: 0x00031E00 File Offset: 0x00030000
		public virtual SkinnedWorldObject Clone()
		{
			return new SkinnedWorldObject
			{
				downResolution = this.downResolution,
				gameObject = this.gameObject,
				transform = this.transform,
				rigidbody = this.rigidbody,
				renderer = this.renderer,
				meshFilter = this.meshFilter,
				mesh = this.mesh,
				updateNormals = this.updateNormals
			};
		}

		// Token: 0x0400085D RID: 2141
		public int downResolution = 1;

		// Token: 0x0400085E RID: 2142
		[HideInInspector]
		public SkinnedMeshRenderer renderer;

		// Token: 0x0400085F RID: 2143
		protected Transform[] boneTransforms;

		// Token: 0x04000860 RID: 2144
		protected BoneWeight[] weights;

		// Token: 0x04000861 RID: 2145
		protected Matrix4x4[] bindPoses;

		// Token: 0x04000862 RID: 2146
		protected Matrix4x4[] boneMatrices;

		// Token: 0x04000863 RID: 2147
		protected Vector3[] vertices;

		// Token: 0x04000864 RID: 2148
		protected Vector3[] localVertices;

		// Token: 0x04000865 RID: 2149
		[HideInInspector]
		public bool isDoneThread = true;

		// Token: 0x04000866 RID: 2150
		protected Mesh bakedMesh;

		// Token: 0x04000867 RID: 2151
		protected Matrix4x4 vertexMatrix = default(Matrix4x4);
	}
}
